﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq.Expressions;
using System.Web.Routing;
// TFSPWD : snd\ifonseka1_cp
namespace InvoiceProject.Helpers
{
    public static class TemplateHelper
    {
        public static void RenderClientTemplate(this HtmlHelper helper, Type _type, string _partialViewName)
        {
            object model = Activator.CreateInstance(_type);
            helper.RenderPartial(_partialViewName, model);
        }
        public static string IDFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expr)
        { //{
            string prefix = helper.ViewData.TemplateInfo.HtmlFieldPrefix;
            string name = ExpressionHelper.GetExpressionText(expr);
            return (prefix + "__" + name).Replace("[", "__").Replace("]", "__");
            // return "";
        } 
    }

    public static class HtmlExtensions
    {
        public static MvcHtmlString RequiredFieldFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
        {
            // Get the metadata for the model
            var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);

            // Check if the field is required
            var isRequired = metadata
                               .ContainerType.GetProperty(metadata.PropertyName)
                               .GetCustomAttributes(typeof(RequiredAttribute), false)
                               .Length == 1;

            // If the field is required generate label with an asterix
            if (isRequired)
            {
                var labelText = "*";

                var tag = new TagBuilder("label");
                tag.MergeAttributes(new Dictionary<string, object> { { "style", "color: red; margin-left: -5px; margin-right: 5px; vertical-align: top;" } });
                tag.SetInnerText(labelText);

                return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
            }

            return null;
        }
        
    
    }
    public static class EnumDropDownList
    {
        public static HtmlString EnumDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> modelExpression, string firstElement)
        {
            var typeOfProperty = modelExpression.ReturnType;
            if (!typeOfProperty.IsEnum)
                throw new ArgumentException(string.Format("Type {0} is not an enum", typeOfProperty));

            var enumValues = new SelectList(Enum.GetValues(typeOfProperty), Enum.GetValues(typeOfProperty));

            foreach (var enumValue in enumValues )
            {
                enumValue.Value = enumValue.Text;
            }

            var htmlString = htmlHelper.DropDownListFor(modelExpression, enumValues, firstElement);


            string html = htmlString.ToHtmlString().Replace("<select",
                                                            "<select data-native-menu='false' data-inline='true'");



            HtmlString result =
                new HtmlString("sd");



            return result;
        }
    }



}